/**
 * 咨询师个人推广测评问卷地址模块功能
 */

var spcode = null;

// 初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	init_bind();
} );

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------

function init_bind()
{
	$( "#btnSubmit" ).click( submit_form );
}

function set_init_data( d )
{
	if( !empty( d ) && !empty( d.spcode ) )
	{
		spcode = d.spcode;

		$( "#sp_date" ).html( d.cdate );
		$( "#sp_user" ).html( d.nickname );
		set_sp_link( d.spcode );

		// 隐藏获取链接
		$( "#btnSubmitBox" ).hide();
		$( "#btnSubmit" ).unbind( "click", submit_form );
	}
	else
	{
		spcode = null;

		$( "#sp_date" ).html( "<em class=\"caption\">--</em>" );
		$( "#sp_user" ).html( "<em class=\"caption\">--</em>" );
		$( "#sp_link_container" ).html( "<em class=\"caption\">--</em>" );
	}
}

//显示推广 链接
function set_sp_link( spcode )
{
	var link = "http://t.jingsiedu.com/t.php?sp=" + spcode;
	$( "#sp_link_container" ).html( "<a id='sp_link'>" + link + "</a>" );
	$( "#sp_link" ).click( sp_link_copy );
}

function sp_link_copy( e )
{
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function submit_form()
{
	var data = {};

	// 组织提交数据
	data.spcode = spcode;
	data.user = user;

	submitter.post( "consult_ask_spcode_go.php", data, function( d )
	{
		if( d.err == 0 )
		{
			infopad.msg( d.msg );
			set_init_data( d );
		}
		else
		{
			infopad.err( d.msg, d.delay );
		}
	}, "json" );
}

//--------------------------------------------------------------------------
//
//	事件处理
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
}
